﻿Imports QLTHUVIEN.DTO
Imports QLTHUVIEN.BUS

Public Class frmNhanVien
    Private Sub layDanhSachNhanVien()
        Dim nvBus As New NhanVienBUS()
        Dim dt As DataTable = nvBus.layNhanVien()
        dtgDanhSachNhanVien.DataSource = dt
    End Sub

    Private Sub loadBangCap()
        Dim bcapbus As BangCapBUS = New BangCapBUS()
        Dim dt As DataTable = bcapbus.LayDanhSach()
        cboBangCap.DataSource = dt
        cboBangCap.DisplayMember = "TenBangCap"
        cboBangCap.ValueMember = "MaBangCap"
    End Sub
    Private Sub loadBoPhan()
        Dim boPhanbus As BoPhanBUS = New BoPhanBUS()
        Dim dt As DataTable = boPhanbus.LayDanhSach()
        cboBoPhan.DataSource = dt
        cboBoPhan.DisplayMember = "TenBoPhan"
        cboBoPhan.ValueMember = "MaBoPhan"
    End Sub

    Private Sub loadChucVu()
        Dim cVubus As ChucVuBUS = New ChucVuBUS()
        Dim dt As DataTable = cVubus.LayDanhSach()
        cboChucVu.DataSource = dt
        cboChucVu.DisplayMember = "TenChucVu"
        cboChucVu.ValueMember = "MaChucVu"
    End Sub

    Private Sub frmNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        layDanhSachNhanVien()
        loadBangCap()
        loadBoPhan()
        loadChucVu()
    End Sub

    Private Sub ganGiaTri()
        txtMaNhanVien.Text = dtgDanhSachNhanVien.CurrentRow.Cells(0).Value.ToString()
        txtTenNhanVien.Text = dtgDanhSachNhanVien.CurrentRow.Cells(1).Value.ToString()
        txtDiaChi.Text = dtgDanhSachNhanVien.CurrentRow.Cells(2).Value.ToString()
        dtpNgaySinh.Text = dtgDanhSachNhanVien.CurrentRow.Cells(3).Value.ToString()
        txtEmail.Text = dtgDanhSachNhanVien.CurrentRow.Cells(4).Value.ToString()
        txtDienThoai.Text = dtgDanhSachNhanVien.CurrentRow.Cells(5).Value.ToString()
        txtTenDangNhap.Text = dtgDanhSachNhanVien.CurrentRow.Cells(6).Value.ToString()
        txtMatKhau.Text = dtgDanhSachNhanVien.CurrentRow.Cells(7).Value.ToString()
        cboBangCap.Text = dtgDanhSachNhanVien.CurrentRow.Cells(8).Value.ToString()
        cboBoPhan.Text = dtgDanhSachNhanVien.CurrentRow.Cells(9).Value.ToString()
        cboChucVu.Text = dtgDanhSachNhanVien.CurrentRow.Cells(10).Value.ToString()
    End Sub

    Private Sub dtgDanhSachNhanVien_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgDanhSachNhanVien.CellClick
        ganGiaTri()
    End Sub

    Private Function layNhanVien(ByRef nhanvienDto As NhanVienDTO) As Boolean
        If txtTenNhanVien.Text = "" Or txtDiaChi.Text = "" Or txtEmail.Text = "" Or txtDienThoai.Text = "" Or txtTenDangNhap.Text = "" Or txtMatKhau.Text = "" Then
            Return False
        End If
        Try
            nhanvienDto.TenNhanVien = txtTenNhanVien.Text
            nhanvienDto.DiaChi = txtDiaChi.Text
            nhanvienDto.NgaySinh = Date.Parse(dtpNgaySinh.Text)
            nhanvienDto.Email = txtEmail.Text
            nhanvienDto.DienThoai = txtDienThoai.Text
            nhanvienDto.TenDangNhap = txtTenDangNhap.Text
            nhanvienDto.MatKhau = txtMatKhau.Text
            nhanvienDto.MaBangCap = cboBangCap.SelectedValue
            nhanvienDto.MaBoPhan = cboBoPhan.SelectedValue
            nhanvienDto.MaChucVu = cboChucVu.SelectedValue
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnThemNhanVien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemNhanVien.Click
        Dim nhanVienDto As NhanVienDTO = New NhanVienDTO()
        If layNhanVien(nhanVienDto) = False Then
            MessageBox.Show("Kiem tra lai du lieu nhap")
            Return
        End If

        Dim nvienBus As New NhanVienBUS()
        nvienBus.ThemNhanVien(nhanVienDto)

        layDanhSachNhanVien()
    End Sub

    Private Sub btnSuaNhanVien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaNhanVien.Click
        If txtMaNhanVien.Text = "" Then
            Return
        End If
        Dim nhanVienDto As NhanVienDTO = New NhanVienDTO()
        nhanVienDto.MaNhanVien = Integer.Parse(txtMaNhanVien.Text)
        If layNhanVien(nhanVienDto) = False Then
            MessageBox.Show("Kiem tra lai du lieu nhap")
            Return
        End If

        Dim nvienBus As New NhanVienBUS()
        nvienBus.SuaNhanVien(nhanVienDto)

        layDanhSachNhanVien()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim kq = MessageBox.Show("Ban co muon Thoat hay khong", "QLTHUVIEN", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If kq = Windows.Forms.DialogResult.No Then
            Return
        End If

        If txtMaNhanVien.Text = "" Then
            Return
        End If

        Dim nhanVienDto As NhanVienDTO = New NhanVienDTO()
        nhanVienDto.MaNhanVien = Integer.Parse(txtMaNhanVien.Text)
        If layNhanVien(nhanVienDto) = False Then
            MessageBox.Show("Kiem tra lai nhan vien can Xoa")
            Return
        End If
        ''danh  dau xoa
        nhanVienDto.IsDelete = True

        Dim nvienBus As New NhanVienBUS()
        nvienBus.XoaNhanVien(nhanVienDto)

        layDanhSachNhanVien()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Dim kq = MessageBox.Show("Ban co muon Thoat hay khong", "QLTHUVIEN", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If kq = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If

    End Sub
End Class